#ifndef __SMS_SERVICE__
#define __SMS_SERVICE__

#include "sms_helper.h"
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <netdb.h>
#include <thread>
#include <mutex>


namespace sms {


class Service
{

public:
    Service(
        std::string request_url,
        std::string request_type,
        std::string response_type,
        std::function<nlohmann::json(nlohmann::json)> callable,
        std::string ip="127.0.0.1",
        int port=9094
    );
    ~Service();
    void kill();
    void recv_loop();
    void send_loop();
    void join();
    nlohmann::json cvimg2sms_mem(const cv::Mat& cvimg, const std::string frame_id="camera");

private:
    bool _link();
    void _heartbeat();
    void _close_socket();
    void _parse_msg(std::string msg);

    std::string _request_url;
    std::string _request_type;
    std::string _response_type;
    std::function<nlohmann::json(nlohmann::json)> _callable;
    std::string _ip;
    int _port;
    std::mutex _send_mtx;

    double _last_send_time;
    bool _force_quit;
    bool _heartbeat_running;
    bool _running;

    int _client_socket;
    struct sockaddr_in _server_addr;
    char* _buf;

    std::string _last_msg;
    int _last_msg_len;

    std::thread* _recv_t;
    std::thread* _send_t;

    bool _use_shm;
    std::string _shm_name;
};


}

#endif
